package com.cskaoyan.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.cskaoyan.mall.commons.util.jwt.JwtTokenUtils;
import com.cskaoyan.user.constants.UserRetCode;
import com.cskaoyan.user.converter.MemberConverter;
import com.cskaoyan.user.converter.UserConverterMapper;
import com.cskaoyan.user.dal.entitys.Member;
import com.cskaoyan.user.dal.persistence.MemberMapper;
import com.cskaoyan.user.dto.LoginStateResponse;
import com.cskaoyan.user.dto.UserInfoDto;
import com.cskaoyan.user.dto.UserLoginRequest;
import com.cskaoyan.user.dto.UserLoginResponse;
import com.cskaoyan.user.service.ILoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

/**
 * @program: duolaimall
 * @description: 登录服务实现，
 * @author: zhong
 * @create: 2022-07-30 10:32
 **/

@Slf4j
@Service
public class LoginServiceImpl implements ILoginService {
    @Autowired
    MemberMapper memberMapper;
    @Autowired
    MemberConverter memberConverter;
    @Autowired
    UserConverterMapper userConverterMapper;

    @Override
    public UserLoginResponse validateUser(UserLoginRequest request) {
        Member user = new Member();
        user.setUsername(request.getUserName());
        String pwdKey = DigestUtils.md5DigestAsHex(request.getPassword().getBytes());
        log.info("密码:{},密码密文:{}",request.getPassword(),pwdKey);
        user.setPassword(pwdKey);
        Member member = memberMapper.selectOne(user);

        UserLoginResponse userLoginResponse = new UserLoginResponse();
        if (member == null) {
            userLoginResponse.setCode(UserRetCode.USERORPASSWORD_ERRROR.getCode());
            userLoginResponse.setMsg(UserRetCode.USERORPASSWORD_ERRROR.getMessage());
        } else if (!member.getIsVerified().equals("Y")) {
            userLoginResponse.setCode(UserRetCode.USER_ISVERFIED_ERROR.getCode());
            userLoginResponse.setMsg(UserRetCode.USER_ISVERFIED_ERROR.getMessage());
        } else {
            userLoginResponse = userConverterMapper.converter(member);
            userLoginResponse.setCode(UserRetCode.SUCCESS.getCode());
            userLoginResponse.setMsg(UserRetCode.SUCCESS.getMessage());

            UserInfoDto userInfo = new UserInfoDto();
            userInfo.setUid(String.valueOf(member.getId()));
            userInfo.setUsername(member.getUsername());
            String json = JSON.toJSON(userInfo).toString();
            log.info(json);
            String token = JwtTokenUtils.builder().msg(json).build().creatJwtToken();
            String freeMsg = JwtTokenUtils.builder().token(token).build().freeJwt();
            log.info("解密信息:{},生成的token密文:{}", freeMsg, token);
            userLoginResponse.setToken(token);
        }
        return userLoginResponse;
    }

    @Override
    public LoginStateResponse validateLogin(String token) {
        LoginStateResponse loginStateResponse = new LoginStateResponse();
        String freeMsg = JwtTokenUtils.builder().token(token).build().freeJwt();
        UserInfoDto userInfo = JSONArray.parseObject(freeMsg, UserInfoDto.class);
        String targetId = userInfo.getUid();
        log.info("查询userId:{}",targetId);
        Member target = new Member();
        target.setId(Long.valueOf(targetId));
        target.setIsVerified("Y");
        Member member = memberMapper.selectOne(target);
        if (member == null) {
            log.info("id不存在或未激活");
            loginStateResponse.setCode(UserRetCode.USER_INFOR_INVALID.getCode());
            loginStateResponse.setMsg(UserRetCode.USER_INFOR_INVALID.getMessage());
            return loginStateResponse;
        }
        loginStateResponse.setUid(member.getId());
        loginStateResponse.setUsername(member.getUsername());
        //loginStateResponse.setFile(member.getFile());
        loginStateResponse.setCode(UserRetCode.SUCCESS.getCode());
        loginStateResponse.setMsg(UserRetCode.SUCCESS.getMessage());
        return loginStateResponse;
    }
}
